package Interfaz;


import java.util.ArrayList;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;

import DTO.DTOGestionarResultado;
import DTO.DTOSets;
import Excepcion.CompetenciaNoPermiteEmpateException;
import Excepcion.EmpateEnEliminacionException;
import Excepcion.EmpateSetException;
import Excepcion.EspaciosVaciosException;
import Excepcion.FaltanPartidosAnterioresException;
import Excepcion.PuntajeNegativoException;
import Excepcion.PuntajeOverflowException;
import Excepcion.ResultadoNoModificableException;
import Negocios.ControlFixture;


public class CU018_set_v2 {
	private static Text text;
	private static Text text_1;
	private static Text text_2;
	private static Text text_3;
	private static Text text_4;
	private static Text text_5;
	private static Text text_6;
	private static Text text_7;
	private static Text text_8;
	private static Text text_9;
	private static Text text_10;
	private static Text text_11;
	private static Text text_12;
	private static Text text_13;
	private static Text text_14;
	private static Text text_15;
	private static Text text_16;
	private static Text text_17;
	CU019 pantallaAnterior;
	int idCompetencia;
	int idFecha;
	int idPartido;
	DTOSets dtoSets;
	protected Shell shlSets;
	
	public CU018_set_v2(int idCompetencia, CU019 pantallaAnterior,
			int idFecha, int idPartido){
		this.idCompetencia = idCompetencia;
		this.pantallaAnterior = pantallaAnterior;
		this.idFecha = idFecha;
		this.idPartido = idPartido;
		
		ControlFixture controlFixture = new ControlFixture();
		dtoSets = controlFixture.getDTOSets(idPartido);
	}

	public static void main(String[] args) {
		try {
			CU018_set_v2 window = new CU018_set_v2(0, null, 0, 0);
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void open() {
		
		Display display=Display.getDefault();
		shlSets=new Shell((SWT.CLOSE | SWT.TITLE | SWT.MIN));
		shlSets.setText("Sets");
		shlSets.setSize(576, 334);
		shlSets.addListener(SWT.Close, new Listener() {
			public void handleEvent(Event event) {
				pantallaAnterior.actualizar();
				pantallaAnterior.shlFixture.setVisible(true);
				pantallaAnterior.shlFixture.forceFocus();
			}
		});
		
		String p1="";
		String p2="";
		if((dtoSets.getParticipante1()!=null)&&(dtoSets.getParticipante2()!=null)){
		p1=dtoSets.getParticipante1();
		p2=dtoSets.getParticipante2();
		}

		ControlFixture ctrlFixture = new ControlFixture();

		int cantidadDeSets = ctrlFixture.getCantidadSets(this.idCompetencia);
		
		System.out.println(cantidadDeSets);
		
		Composite composite = new Composite(shlSets, SWT.NONE);
		composite.setBounds(10, 10, 180, 82);
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setBounds(10, 23, 116, 13);
		lblNewLabel.setText(p1);
		
		Label lblSet = new Label(composite, SWT.NONE);
		lblSet.setBounds(10, 4, 31, 13);
		lblSet.setText("Set 01");
		
		text = new Text(composite, SWT.BORDER);
		text.setEditable(false);
		text.setBounds(132, 20, 38, 19);
		
		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setBounds(10, 56, 116, 13);
		lblNewLabel_1.setText(p2);
		
		text_1 = new Text(composite, SWT.BORDER);
		text_1.setEditable(false);
		text_1.setBounds(132, 53, 38, 19);
		
		Composite composite_1 = new Composite(shlSets, SWT.NONE);
		composite_1.setBounds(196, 10, 180, 82);
		
		Label lblP = new Label(composite_1, SWT.NONE);
		lblP.setText(p1);
		lblP.setBounds(10, 23, 116, 13);
		
		Label lblSet_1 = new Label(composite_1, SWT.NONE);
		lblSet_1.setText("Set 02");
		lblSet_1.setBounds(10, 4, 31, 13);
		
		text_2 = new Text(composite_1, SWT.BORDER);
		text_2.setEditable(false);
		text_2.setBounds(132, 20, 38, 19);
		
		Label lblP_1 = new Label(composite_1, SWT.NONE);
		lblP_1.setText(p2);
		lblP_1.setBounds(10, 56, 116, 13);
		
		text_3 = new Text(composite_1, SWT.BORDER);
		text_3.setEditable(false);
		text_3.setBounds(132, 53, 38, 19);
		
		Composite composite_2 = new Composite(shlSets, SWT.NONE);
		composite_2.setBounds(382, 10, 180, 82);
		
		Label lblP_2 = new Label(composite_2, SWT.NONE);
		lblP_2.setText(p1);
		lblP_2.setBounds(10, 23, 116, 13);
		
		Label lblSet_2 = new Label(composite_2, SWT.NONE);
		lblSet_2.setText("Set 03");
		lblSet_2.setBounds(10, 4, 31, 13);
		
		text_4 = new Text(composite_2, SWT.BORDER);
		text_4.setEditable(false);
		text_4.setBounds(132, 20, 38, 19);
		
		Label lblP_3 = new Label(composite_2, SWT.NONE);
		lblP_3.setText(p2);
		lblP_3.setBounds(10, 56, 116, 13);
		
		text_5 = new Text(composite_2, SWT.BORDER);
		text_5.setEditable(false);
		text_5.setBounds(132, 53, 38, 19);
		
		Composite composite_3 = new Composite(shlSets, SWT.NONE);
		composite_3.setBounds(10, 98, 180, 82);
		
		Label lblP_4 = new Label(composite_3, SWT.NONE);
		lblP_4.setText(p1);
		lblP_4.setBounds(10, 23, 116, 13);
		
		Label lblSet_3 = new Label(composite_3, SWT.NONE);
		lblSet_3.setText("Set 04");
		lblSet_3.setBounds(10, 4, 31, 13);
		
		text_6 = new Text(composite_3, SWT.BORDER);
		text_6.setEditable(false);
		text_6.setBounds(132, 20, 38, 19);
		
		Label lblP_5 = new Label(composite_3, SWT.NONE);
		lblP_5.setText(p2);
		lblP_5.setBounds(10, 56, 116, 13);
		
		text_7 = new Text(composite_3, SWT.BORDER);
		text_7.setEditable(false);
		text_7.setBounds(132, 53, 38, 19);
		
		Composite composite_4 = new Composite(shlSets, SWT.NONE);
		composite_4.setBounds(196, 98, 180, 82);
		
		Label lblP_6 = new Label(composite_4, SWT.NONE);
		lblP_6.setText(p1);
		lblP_6.setBounds(10, 23, 116, 13);
		
		Label lblSet_4 = new Label(composite_4, SWT.NONE);
		lblSet_4.setText("Set 05");
		lblSet_4.setBounds(10, 4, 31, 13);
		
		text_8 = new Text(composite_4, SWT.BORDER);
		text_8.setEditable(false);
		text_8.setBounds(132, 20, 38, 19);
		
		Label lblP_7 = new Label(composite_4, SWT.NONE);
		lblP_7.setText(p2);
		lblP_7.setBounds(10, 56, 116, 13);
		
		text_9 = new Text(composite_4, SWT.BORDER);
		text_9.setEditable(false);
		text_9.setBounds(132, 53, 38, 19);
		
		Composite composite_5 = new Composite(shlSets, SWT.NONE);
		composite_5.setBounds(382, 98, 180, 82);
		
		Label lblP_8 = new Label(composite_5, SWT.NONE);
		lblP_8.setText(p1);
		lblP_8.setBounds(10, 23, 116, 13);
		
		Label lblSet_5 = new Label(composite_5, SWT.NONE);
		lblSet_5.setText("Set 06");
		lblSet_5.setBounds(10, 4, 31, 13);
		
		text_10 = new Text(composite_5, SWT.BORDER);
		text_10.setEditable(false);
		text_10.setBounds(132, 20, 38, 19);
		
		Label lblP_9 = new Label(composite_5, SWT.NONE);
		lblP_9.setText(p2);
		lblP_9.setBounds(10, 56, 116, 13);
		
		text_11 = new Text(composite_5, SWT.BORDER);
		text_11.setEditable(false);
		text_11.setBounds(132, 53, 38, 19);
		
		Composite composite_6 = new Composite(shlSets, SWT.NONE);
		composite_6.setBounds(10, 186, 180, 82);
		
		Label lblP_10 = new Label(composite_6, SWT.NONE);
		lblP_10.setText(p1);
		lblP_10.setBounds(10, 23, 116, 13);
		
		Label lblSet_6 = new Label(composite_6, SWT.NONE);
		lblSet_6.setText("Set 07");
		lblSet_6.setBounds(10, 4, 31, 13);
		
		text_12 = new Text(composite_6, SWT.BORDER);
		text_12.setEditable(false);
		text_12.setBounds(132, 20, 38, 19);
		
		Label lblP_11 = new Label(composite_6, SWT.NONE);
		lblP_11.setText(p2);
		lblP_11.setBounds(10, 56, 116, 13);
		
		text_13 = new Text(composite_6, SWT.BORDER);
		text_13.setEditable(false);
		text_13.setBounds(132, 53, 38, 19);
		
		Composite composite_7 = new Composite(shlSets, SWT.NONE);
		composite_7.setBounds(196, 186, 180, 82);
		
		Label lblP_12 = new Label(composite_7, SWT.NONE);
		lblP_12.setText(p1);
		lblP_12.setBounds(10, 23, 116, 13);
		
		Label lblSet_7 = new Label(composite_7, SWT.NONE);
		lblSet_7.setText("Set 08");
		lblSet_7.setBounds(10, 4, 31, 13);
		
		text_14 = new Text(composite_7, SWT.BORDER);
		text_14.setEditable(false);
		text_14.setBounds(132, 20, 38, 19);
		
		Label lblP_13 = new Label(composite_7, SWT.NONE);
		lblP_13.setText(p2);
		lblP_13.setBounds(10, 56, 116, 13);
		
		text_15 = new Text(composite_7, SWT.BORDER);
		text_15.setEditable(false);
		text_15.setBounds(132, 53, 38, 19);
		
		Composite composite_8 = new Composite(shlSets, SWT.NONE);
		composite_8.setBounds(382, 186, 180, 82);
		
		Label lblP_14 = new Label(composite_8, SWT.NONE);
		lblP_14.setText(p1);
		lblP_14.setBounds(10, 23, 116, 13);
		
		Label lblSet_8 = new Label(composite_8, SWT.NONE);
		lblSet_8.setText("Set 09");
		lblSet_8.setBounds(10, 4, 31, 13);
		
		text_16 = new Text(composite_8, SWT.BORDER);
		text_16.setEditable(false);
		text_16.setBounds(132, 20, 38, 19);
		
		Label lblP_15 = new Label(composite_8, SWT.NONE);
		lblP_15.setText(p2);
		lblP_15.setBounds(10, 56, 116, 13);
		
		text_17 = new Text(composite_8, SWT.BORDER);
		text_17.setEditable(false);
		text_17.setBounds(132, 53, 38, 19);
		
		//int cantidadDeSets = sets.getSets().size();
		
		if(cantidadDeSets>=3){
			
			if(dtoSets.getEdicion()){
			
				text.setText(dtoSets.getSets().get(0).getPuntaje1()+"");
				text_1.setText(dtoSets.getSets().get(0).getPuntaje2()+"");
				text_2.setText(dtoSets.getSets().get(1).getPuntaje1()+"");
				text_3.setText(dtoSets.getSets().get(1).getPuntaje2()+"");
				text_4.setText(dtoSets.getSets().get(2).getPuntaje1()+"");
				text_5.setText(dtoSets.getSets().get(2).getPuntaje2()+"");	
				
			}
			
			text.setEditable(true);
			text_1.setEditable(true);
			text_2.setEditable(true);
			text_3.setEditable(true);
			text_4.setEditable(true);
			text_5.setEditable(true);	
		}
		if(cantidadDeSets>=5){
			if(dtoSets.getEdicion()){
				text_6.setText(dtoSets.getSets().get(3).getPuntaje2()+"");
				text_7.setText(dtoSets.getSets().get(3).getPuntaje1()+"");
				text_8.setText(dtoSets.getSets().get(4).getPuntaje2()+"");
				text_9.setText(dtoSets.getSets().get(4).getPuntaje1()+"");
			}
			text_6.setEditable(true);
			text_7.setEditable(true);
			text_8.setEditable(true);
			text_9.setEditable(true);
				
		}
		if(cantidadDeSets>=7){
			if(dtoSets.getEdicion()){
				text_10.setText(dtoSets.getSets().get(5).getPuntaje2()+"");
				text_11.setText(dtoSets.getSets().get(5).getPuntaje1()+"");
				text_12.setText(dtoSets.getSets().get(6).getPuntaje2()+"");
				text_13.setText(dtoSets.getSets().get(6).getPuntaje1()+"");
			}
			text_10.setEditable(true);
			text_11.setEditable(true);
			text_12.setEditable(true);
			text_13.setEditable(true);
			
		}
		if(cantidadDeSets>=9){
			if(dtoSets.getEdicion()){
				text_14.setText(dtoSets.getSets().get(7).getPuntaje2()+"");
				text_15.setText(dtoSets.getSets().get(7).getPuntaje1()+"");
				text_16.setText(dtoSets.getSets().get(8).getPuntaje2()+"");
				text_17.setText(dtoSets.getSets().get(8).getPuntaje1()+"");
			}
			text_14.setEditable(true);
			text_15.setEditable(true);
			text_16.setEditable(true);
			text_17.setEditable(true);	
		}
		
		
		Button btnGuardar = new Button(shlSets, SWT.NONE);
		btnGuardar.setBounds(392, 274, 68, 23);
		btnGuardar.setText("Guardar");
		btnGuardar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				
				ControlFixture ctrlFixture2 = new ControlFixture();
				int cantidadDeSets=ctrlFixture2.getCantidadSets(idCompetencia);
				ArrayList<Integer> resultadoP1=new ArrayList<Integer>();
				ArrayList<Integer> resultadoP2=new ArrayList<Integer>();
				if(cantidadDeSets>=3){
					resultadoP1.add(Integer.parseInt(text.getText()));
					resultadoP1.add(Integer.parseInt(text_2.getText()));
					resultadoP1.add(Integer.parseInt(text_4.getText()));
					
					resultadoP2.add(Integer.parseInt(text_1.getText()));
					resultadoP2.add(Integer.parseInt(text_3.getText()));
					resultadoP2.add(Integer.parseInt(text_5.getText()));
				}
				if(cantidadDeSets>=5){
					resultadoP1.add(Integer.parseInt(text_6.getText()));
					resultadoP1.add(Integer.parseInt(text_8.getText()));
					
					resultadoP2.add(Integer.parseInt(text_7.getText()));
					resultadoP2.add(Integer.parseInt(text_9.getText()));
				}
				if(cantidadDeSets>=7){
					resultadoP1.add(Integer.parseInt(text_10.getText()));
					resultadoP1.add(Integer.parseInt(text_12.getText()));
					
					resultadoP2.add(Integer.parseInt(text_11.getText()));
					resultadoP2.add(Integer.parseInt(text_13.getText()));
				}
				if(cantidadDeSets>=9){
					resultadoP1.add(Integer.parseInt(text_14.getText()));
					resultadoP1.add(Integer.parseInt(text_16.getText()));
					
					resultadoP2.add(Integer.parseInt(text_15.getText()));
					resultadoP2.add(Integer.parseInt(text_17.getText()));
				}
				
				DTOGestionarResultado dtoNuevoResultado=new DTOGestionarResultado(idCompetencia,
						idPartido, -1, -1, -1, resultadoP1, resultadoP2);
				
				try {
					ControlFixture controlFixture = new ControlFixture();
					controlFixture.gestionarResultado(dtoNuevoResultado);
					
					MessageBox mensaje = new MessageBox(shlSets, SWT.ICON_INFORMATION);
					mensaje.setMessage("Las puntuaciones han sido guardadas exitosamente.");
					mensaje.open();
					
					shlSets.close();
				}
				catch (ResultadoNoModificableException ex) {
					MessageBox error = new MessageBox(shlSets, SWT.ICON_ERROR);
					error.setMessage(ex.getMensaje());
					error.open();
				}
				catch (EmpateEnEliminacionException ex) {
					MessageBox error = new MessageBox(shlSets, SWT.ICON_ERROR);
					error.setMessage(ex.getMensaje());
					error.open();
				}
				catch (FaltanPartidosAnterioresException ex) {
					MessageBox error = new MessageBox(shlSets, SWT.ICON_ERROR);
					error.setMessage(ex.getMensaje());
					error.open();
				}
				catch (CompetenciaNoPermiteEmpateException ex) {
					MessageBox error = new MessageBox(shlSets, SWT.ICON_ERROR);
					error.setMessage(ex.getMensaje());
					error.open();
				}
				catch (PuntajeNegativoException ex) {
					MessageBox error = new MessageBox(shlSets, SWT.ICON_ERROR);
					error.setMessage(ex.getMensaje());
					error.open();
				}
				catch (EspaciosVaciosException ex) {
					MessageBox error = new MessageBox(shlSets, SWT.ICON_ERROR);
					error.setMessage(ex.getMensaje());
					error.open();
				}
				catch (EmpateSetException ex) {
					MessageBox error = new MessageBox(shlSets, SWT.ICON_ERROR);
					error.setMessage(ex.getMensaje());
					error.open();
				}
				catch (PuntajeOverflowException ex) {
					MessageBox error = new MessageBox(shlSets, SWT.ICON_ERROR);
					error.setMessage(ex.getMensaje());
					error.open();
				}
	
			}
			});
		Button btnCancelar = new Button(shlSets, SWT.NONE);
		btnCancelar.setText("Cancelar");
		btnCancelar.setBounds(484, 274, 68, 23);
		btnCancelar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shlSets.close();
			}
		});
		
		// Centrar ventana
				Monitor primary = display.getPrimaryMonitor();
				Rectangle bounds = primary.getBounds();
				Rectangle rect = shlSets.getBounds();	    
				int x = bounds.x + (bounds.width - rect.width) / 2;
				int y = bounds.y + (bounds.height - rect.height) / 2;	    
				shlSets.setLocation(x, y);
				
		
		shlSets.open();
		while(! shlSets.isDisposed()){
			if(!display.readAndDispatch())display.sleep();
		}
		display.dispose();
	}
	
}
